package com.fuint.common.chainlog;

import cn.hutool.core.util.StrUtil;
import com.fuint.common.util.LogUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Objects;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ChainLog {
    private String title;
    private String paramsLog;
    private List<String> stackLog;
    private String successLog;
    private Throwable errorLog;

    public void pushStack(String stackLog) {
        if(this.stackLog == null) {
            this.stackLog = new java.util.ArrayList<>();
        }
        this.stackLog.add(stackLog);
    }

    public void print() {
        StringBuilder message = new StringBuilder("\n--------" + title + "--------");
        if (StrUtil.isNotEmpty(paramsLog)) {
            message.append("\n").append(paramsLog);
        }
        if (Objects.nonNull(stackLog) && !stackLog.isEmpty()) {
            for (String s : stackLog) {
                message.append("\n").append(s);
            }
        }
        message.append("\n").append(title);
        if (Objects.isNull(errorLog)) {
            message.append("成功");
            if (StrUtil.isNotEmpty(successLog)) {
                message.append(":").append(successLog);
            }
            LogUtils.info(message.toString());
        } else {
            message.append("失败:\n");
            LogUtils.error(message.toString(), errorLog);
        }
    }
}
